package com.cjjd.c.feature.home.chekin.checklist_detail;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.BaseSwipeRefreshFragment;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.CheckInItemBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.feature.dialog.EditTextDialog;
import com.cjjd.c.feature.dialog.ShowTwoChooseDialog;
import com.cjjd.c.feature.mine.order.OrderAdapter;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.DialogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.StringUtil;
import com.cjjd.c.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */
public class CheckInListFragment extends BaseSwipeRefreshFragment implements View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }


    EditText et_name;
    EditText et_mobile;
    private int Customer = 0x00000001;              // 入住人
    private int searchType = 0x00000002;            // 手机号搜索
    private int bookSearchType = 0x00000003;        // 预订码搜索
    private int getType = Customer;

    // 预订码
    private String bookCode;

    @Override
    public Object getTitleString() {
        return "订单信息";
    }


    Button empty_search;

    @Override
    protected void initView() {
        super.initView();
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, (int) (10 * getResources().getDisplayMetrics().density + 0.5), 0, 0);
        listBase.setLayoutParams(layoutParams);
        listBase.setDivider(getResources().getDrawable(R.drawable.order_divider));
        listBase.setDividerHeight((int) (10 * mContext.getResources().getDisplayMetrics().density + 0.5));

        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_check_in, emptyview, false);
        empty_search = (Button) view.findViewById(R.id.empty_search);

        empty_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTwoChooseDialog();
            }
        });

        emptyview.addView(view);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onRequestFinish() {
        super.onRequestFinish();
        setEmptyViewVisiable(true);
    }


    private List<CheckInItemBean> dataList = new ArrayList<>();

    @Override
    public void responseSuccess(Call<HttpResult> call, Response<HttpResult> response) {
        ListResult<CheckInItemBean> checkinList = (ListResult<CheckInItemBean>) response.body().getData();
        if (dataList.size() > 0) {
            dataList.clear();
        }
        dataList.addAll(checkinList.dataList);
        setDataList(dataList);
        if (getType == searchType && dataList.size() == 0) {
            ToastUtil.showToast("查无订单，请核实您的手机号和入住姓名是否准确");
        } else if (getType == bookSearchType && dataList.size() == 0) {
            ToastUtil.showToast("查无订单");
        }

        // 查询的只有一次，刷新操作，切换回登录客户的入住订单查询
        if (getType!=Customer){
            getType = Customer;
        }

    }


    private String name;
    private String mobile;

    private boolean checkCanSearch() {

        name = et_name.getText().toString().trim();
        mobile = et_mobile.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            ToastUtil.showToast("请输入姓名");
            return false;
        }

        if (!StringUtil.isMobile(mobile)) {

            ToastUtil.showToast("请输入正确的手机号");
            return false;
        }

        return true;

    }


    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
        rightButton.setVisibility(View.VISIBLE);
        Drawable searchDrawable = mContext.getResources().getDrawable(R.drawable.icon_serch);
        searchDrawable.setBounds(0, 0, searchDrawable.getMinimumWidth(), searchDrawable.getMinimumHeight());
        rightButton.setCompoundDrawables(null, null, searchDrawable, null);
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTwoChooseDialog();

            }
        });
    }

    /**
     * 展示两个选择
     */
    private void showTwoChooseDialog() {
        new ShowTwoChooseDialog(mContext, "订单查询", new ShowTwoChooseDialog.OnCloseListener() {
            @Override
            public void OnBookSearchClick() {
                showBookDialog();
            }

            @Override
            public void OnPhoneSearchClick() {
                showCaptchaDialog();
            }

            @Override
            public void Onclose() {

            }
        }).show();

    }


    /**
     * 预订码查询
     */
    private void showBookDialog() {
        new EditTextDialog(mContext, "订单查询", "请输入预订码", new EditTextDialog.OnCloseListener() {
            @Override
            public void OnClick(String message) {
                bookCode = message.trim();
                if (TextUtils.isEmpty(bookCode)) {
                    ToastUtil.showLongToast(mContext, "请输入预订码");
                    return;
                }
                // 先赋值
                getType = bookSearchType;
                onRefresh();

                // 后换原
                bookCode = "";


            }
        }).show();

    }

    /**
     * 手机号查询
     */
    private void showCaptchaDialog() {
        int resId[] = {R.id.et_name, R.id.et_mobile, R.id.checin_next};

        final LinearLayout dialogView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.dialog_checkin, emptyview, false);
        et_name = (EditText) dialogView.findViewById(R.id.et_name);
        et_mobile = (EditText) dialogView.findViewById(R.id.et_mobile);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        DialogUtil.showCustomAlert(mContext, dialogView, layoutParams, resId, new DialogUtil.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final View view) {

                switch (view.getId()) {
                    case R.id.checin_next:

                        if (getType != searchType) {
                            getType = searchType;
                        }

                        if (checkCanSearch()) {
                            onRefresh();
                            dialog.dismiss();
                        }
                        break;
//                    case R.id.quit:
//                        dialog.dismiss();
//                        break;

                }


            }
        });
    }

    @Override
    public MyBaseAdapter getAdapter() {
        return new OrderAdapter(mContext, OrderAdapter.CHECK_IN);
    }

    @Override
    public Call<HttpResult> getCall() {

        if (getType == Customer) {
            // 入住
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("nowPage", PAGE + "");
            h1.put("pageShow", PAGE_SIZE + "");

            return service.getCustomerReserveOrderList(JsonUtils.convertParam("getCustomerReserveOrderList", h1));

        } else if (getType == bookSearchType) {
            // 预订码查询
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("nowPage", PAGE + "");
            h1.put("pageShow", PAGE_SIZE + "");
            h1.put("ydCode", bookCode);

            return service.ydCodeCustomerReserveOrderList(JsonUtils.convertParam("ydCodeCustomerReserveOrderList", h1));

        } else {
            // 手机号查询
            HashMap<String, Object> h1 = new HashMap<>();
            h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
            h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
            h1.put("mobile", mobile);
            h1.put("name", name);
            h1.put("nowPage", PAGE + "");
            h1.put("pageShow", PAGE_SIZE + "");

            return service.searchCustomerReserveOrderList(JsonUtils.convertParam("searchCustomerReserveOrderList", h1));

        }

    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (EventBusKeys.hadPayCheckIn.equals(event)) {
            mContext.finish();
        }
    }
}
